package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.sdk.universalimageloader.core.ImageLoader;
import com.taptech.doufu.util.BitmapUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.bean.TagsBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.personalcenter.ContentTagsViewGroup;
import com.taptech.doufu.util.ColorString;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;

import java.util.List;

public class TTHomeNovelAdapter extends BaseListAdapter
{


    private Context mContext;
    private LayoutInflater mLayoutInflater;
    protected ImageLoader imageLoader = null;
    private HomeTopBean bean;
    private TagsBean[] titleLabels;
    //	private String titleLabel="";
    static int width, space, rightSpace;
    private Bitmap defaultBitmap = null;
    private boolean newFlag;


    public TTHomeNovelAdapter(Context mContext, boolean newFlag)
    {

        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        if (width == 0)
        {
            space = ScreenUtil2.dip2px(6);
            rightSpace = ScreenUtil2.dip2px(8);
            int macthWidth = ScreenUtil2.SCREEN_PX_WIDTH - space * 3 - 2 * rightSpace;
            width = macthWidth / 3;
        }
        defaultBitmap = BitmapUtil.decodeSampledBitmapFromResource(mContext.getResources(), R.drawable.bg_home_photo_default, width, width);
        this.newFlag = newFlag;
    }
    //	public TTHomeNovelAdapter(){
    //
    //	}

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        NovelHolder novelHolder = null;

        if (convertView == null)
        {
            convertView = mLayoutInflater.inflate(R.layout.home_novel_item, null);
            novelHolder = new NovelHolder();
            novelHolder.novelTitle = (TextView) convertView.findViewById(R.id.home_novel_item_title);
            novelHolder.novelAuthor = (TextView) convertView.findViewById(R.id.home_novel_item_author);
            novelHolder.novelUpdateTime = (TextView) convertView.findViewById(R.id.home_novel_item_update_time);
            novelHolder.novelReadTimes = (TextView) convertView.findViewById(R.id.home_novel_item_read_times);
            novelHolder.flowerCount = (TextView) convertView.findViewById(R.id.home_novel_item_flower_count);
            novelHolder.commentCount = (TextView) convertView.findViewById(R.id.home_novel_item_comment_count);
            novelHolder.novelUpLoader = (TextView) convertView.findViewById(R.id.home_novel_item_uploader);
            novelHolder.novelDes = (TextView) convertView.findViewById(R.id.home_novel_item_des);
            novelHolder.novelComment = (TextView) convertView.findViewById(R.id.home_novel_item_comment);
            novelHolder.novelAuthorImg = (ImageView) convertView.findViewById(R.id.home_novel_item_authorimg);
            novelHolder.novelLog = (ImageView) convertView.findViewById(R.id.home_novel_item_log);
            novelHolder.novelStatus = (TextView) convertView.findViewById(R.id.home_novel_item_continue);
            novelHolder.videoIcon = (ImageView) convertView.findViewById(R.id.home_novel_item_video_icon);
            novelHolder.novelLabelLayout = (LinearLayout) convertView.findViewById(R.id.home_novel_item_label_layout);
            novelHolder.imgVip = (ImageView) convertView.findViewById(R.id.img_home_novel_item_log);

            novelHolder.novelTagList = (ContentTagsViewGroup) convertView.findViewById(R.id.home_item_taglist);
            novelHolder.bottom_layout = convertView.findViewById(R.id.home_novel_item_bottom_layout);
            convertView.setTag(novelHolder);
        } else
        {
            novelHolder = (NovelHolder) convertView.getTag();
        }
        novelHolder.novelTagList.setType(Constant.OBJECT_TYPE_NOVEL);
        bean = (HomeTopBean) getDataSource().get(position);
        if (bean.getTags() != null && bean.getTags().length > 0)
        {

            titleLabels = bean.getTags();
            String[] tagStrings = new String[bean.getTags().length];
            for (int i = 0; i < titleLabels.length; i++)
            {
                tagStrings[i] = titleLabels[i].getName();
            }
            novelHolder.novelTagList.setTextTags(mContext, tagStrings);
        }

        novelHolder.novelTitle.setText(bean.getTitle());

        if("1".equals(bean.getIs_vip())){
            novelHolder.imgVip.setVisibility(View.VISIBLE);
        }else{
            novelHolder.imgVip.setVisibility(View.GONE);
        }

        if (bean.getRead_times() != null)
        {
            novelHolder.novelReadTimes.setText(bean.getRead_times());
        }
        if (bean.getFlowerCount() != null)
        {
            novelHolder.flowerCount.setText(bean.getFlowerCount());
        }
        if (bean.getComment_times() != null)
        {
            novelHolder.commentCount.setText(bean.getComment_times());
        }

        //作者名
        if (bean.getAuthor() != null && !bean.getAuthor().equals(""))
        {
            novelHolder.novelAuthor.setText("作者: " + bean.getAuthor());
        } else
        {
            novelHolder.novelAuthor.setText("作者: 佚名");
        }
        //简介
        novelHolder.novelDes.setText(bean.getDes());
        //new AtUtil(novelHolder.novelDes, bean.getDes(),Constant.AT_CLICK);
        //上传者图
        String imageUrl = "";


        if (bean.getImages() != null && bean.getImages().length > 0)
        {

            imageUrl = bean.getImages()[0].getImgUrl();
        }
        novelHolder.novelLog.setImageBitmap(defaultBitmap);
        ImageManager.displayImage(novelHolder.novelLog, imageUrl, 0);

        if (bean!=null && "1".equals(bean.getStatus()))
        {
            DiaobaoUtil.novelFinish(novelHolder.novelStatus, bean.getFinished());
            novelHolder.bottom_layout.setVisibility(View.VISIBLE);
            novelHolder.novelUpdateTime.setTextColor(Color.parseColor(ColorString.GREY_TEXT));
            novelHolder.novelStatus.setVisibility(View.VISIBLE);
            if (bean.getUpdate_time() != null)
            {
                novelHolder.novelUpdateTime.setText("更新：" + DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getUpdate_time())));
            } else if (bean.getAdd_time() != null)
            {
                novelHolder.novelUpdateTime.setText("更新：" + DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getAdd_time())));
            }
        } else
        {
            novelHolder.bottom_layout.setVisibility(View.GONE);
            novelHolder.novelUpdateTime.setText("未发布连载中");
            novelHolder.novelUpdateTime.setTextColor(Color.parseColor(ColorString.RED_TEXT));
            novelHolder.novelStatus.setVisibility(View.GONE);
        }
        return convertView;
    }


    public static class NovelHolder
    {
        public RelativeLayout mItemLayout;
        public TextView novelTitle;
        public TextView novelAuthor;
        public TextView novelUpLoader;
        public TextView novelComment;
        public TextView novelDes;
        public LinearLayout novelLabelLayout;
        public ContentTagsViewGroup novelTagList;
        public TextView novelUpdateTime;
        public TextView novelReadTimes;
        public TextView flowerCount;
        public TextView commentCount;
        public ImageView novelAuthorImg;
        public ImageView novelLog;
        public ImageView videoIcon;
        public ImageView imgVip;
        //小说更新/连载状态
        public TextView novelStatus;
        public View bottom_layout;
    }

    public void addHeaderView(boolean headFlag)
    {
        this.mHasHeaderView = headFlag;
    }

    public class ContentViewHolder {
        public TextView commentCount;
    }

    public HomeTopBean getBean(int postion)
    {
        HomeTopBean topBean = null;
        if (this.mHasHeaderView)
        {
            postion = postion - 1;
        }
        List<Object> mainList = getDataSource();
        if (postion >= 0 && mainList != null && postion < mainList.size())
        {
            topBean = (HomeTopBean) mainList.get(postion);
        }
        return topBean;

    }


}
